package com.cloudea.learn.exception;

/**
 * 维度处理异常
 * @author cloudea
 * @date 2021/12/28
 */
public class DimensionException extends RuntimeException {

    public DimensionException(int[] received){
        super(String.format("Tensor维度出现错误：维度: %s",
                dimensionToString(received)));
    }

    public DimensionException(int[] expected, int[] received){
        super(String.format("Tensor维度出现错误：期待维度%s，实际维度: %s",
               dimensionToString(expected),
               dimensionToString(received)));
    }

    /**
     * 把维度数组转变成字符串表示
     * @param dimensions 维度数字
     * @return 字符串
     */
    public static String dimensionToString(int[] dimensions){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("(");
        for(int i = 0; i < dimensions.length; i++){
           if (i != 0){
               stringBuilder.append(",");
           }
           stringBuilder.append(dimensions[i]);
        }

        stringBuilder.append(")");
        return stringBuilder.toString();
    }
}
